import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;

import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.parser.ParserDelegator;


public class MyParserDelegator extends ParserDelegator {

	private static final long serialVersionUID = 1L;
	
	public String file;
	
	private static Map<String, Tag> tagMap;
	private static Map<String, Attribute> attrMap;
	
    static {
        Map<String, Tag> tagMap = new HashMap<String, Tag>();
		tagMap.put("a", HTML.Tag.A);
		tagMap.put("address", HTML.Tag.ADDRESS);
		tagMap.put("applet", HTML.Tag.APPLET);
		tagMap.put("b", HTML.Tag.B);
		tagMap.put("basefont", HTML.Tag.BASEFONT);
		tagMap.put("big", HTML.Tag.BIG);
		tagMap.put("blockquote", HTML.Tag.BLOCKQUOTE);
		tagMap.put("body", HTML.Tag.BODY);
		tagMap.put("br", HTML.Tag.BR);
		tagMap.put("caption", HTML.Tag.CAPTION);
		tagMap.put("center", HTML.Tag.CENTER);
		tagMap.put("cite", HTML.Tag.CITE);
		tagMap.put("code", HTML.Tag.CODE);
		tagMap.put("dd", HTML.Tag.DD);
		tagMap.put("dfn", HTML.Tag.DFN);
		tagMap.put("dir", HTML.Tag.DIR);
		tagMap.put("div", HTML.Tag.DIV);
		tagMap.put("dl", HTML.Tag.DL);
		tagMap.put("dt", HTML.Tag.DT);
		tagMap.put("em", HTML.Tag.EM);
		tagMap.put("font", HTML.Tag.FONT);
		tagMap.put("form", HTML.Tag.FORM);
		tagMap.put("frameset", HTML.Tag.FRAMESET);
		tagMap.put("h1", HTML.Tag.H1);
		tagMap.put("h2", HTML.Tag.H2);
		tagMap.put("h3", HTML.Tag.H3);
		tagMap.put("h4", HTML.Tag.H4);
		tagMap.put("h5", HTML.Tag.H5);
		tagMap.put("h6", HTML.Tag.H6);
		tagMap.put("head", HTML.Tag.HEAD);
		tagMap.put("hr", HTML.Tag.HR);
		tagMap.put("html", HTML.Tag.HTML);
		tagMap.put("i", HTML.Tag.I);
		tagMap.put("img", HTML.Tag.IMG);
		tagMap.put("kbd", HTML.Tag.KBD);
		tagMap.put("li", HTML.Tag.LI);
		tagMap.put("map", HTML.Tag.MAP);
		tagMap.put("menu", HTML.Tag.MENU);
		tagMap.put("noframes", HTML.Tag.NOFRAMES);
		tagMap.put("object", HTML.Tag.OBJECT);
		tagMap.put("ol", HTML.Tag.OL);
		tagMap.put("option", HTML.Tag.OPTION);
		tagMap.put("p", HTML.Tag.P);
		tagMap.put("pre", HTML.Tag.PRE);
		tagMap.put("s", HTML.Tag.S);
		tagMap.put("samp", HTML.Tag.SAMP);
		tagMap.put("script", HTML.Tag.SCRIPT);
		tagMap.put("select", HTML.Tag.SELECT);
		tagMap.put("small", HTML.Tag.SMALL);
		tagMap.put("span", HTML.Tag.SPAN);
		tagMap.put("strike", HTML.Tag.STRIKE);
		tagMap.put("strong", HTML.Tag.STRONG);
		tagMap.put("style", HTML.Tag.STYLE);
		tagMap.put("sub", HTML.Tag.SUB);
		tagMap.put("sup", HTML.Tag.SUP);
		tagMap.put("table", HTML.Tag.TABLE);
		tagMap.put("td", HTML.Tag.TD);
		tagMap.put("textarea", HTML.Tag.TEXTAREA);
		tagMap.put("th", HTML.Tag.TH);
		tagMap.put("title", HTML.Tag.TITLE);
		tagMap.put("tr", HTML.Tag.TR);
		tagMap.put("tt", HTML.Tag.TT);
		tagMap.put("u", HTML.Tag.U);
		tagMap.put("ul", HTML.Tag.UL);
		tagMap.put("var", HTML.Tag.VAR);
        MyParserDelegator.tagMap = Collections.unmodifiableMap(tagMap);
        
        Map<String, Attribute> attrMap = new HashMap<String, Attribute>();
        attrMap.put("align", HTML.Attribute.ALIGN);
        attrMap.put("alt", HTML.Attribute.ALT);
        attrMap.put("cellspacing", HTML.Attribute.CELLSPACING);
        attrMap.put("cellpadding", HTML.Attribute.CELLPADDING);
        attrMap.put("colspan", HTML.Attribute.COLSPAN);
        attrMap.put("content", HTML.Attribute.CONTENT);
        attrMap.put("href", HTML.Attribute.HREF);
        attrMap.put("http-equiv", HTML.Attribute.HTTPEQUIV);
        attrMap.put("id", HTML.Attribute.ID);
        attrMap.put("language", HTML.Attribute.LANGUAGE);
        attrMap.put("media", null);
        attrMap.put("name", HTML.Attribute.NAME);
        attrMap.put("rel", HTML.Attribute.REL);
        attrMap.put("style", HTML.Attribute.STYLE);
        attrMap.put("src", HTML.Attribute.SRC);
        attrMap.put("style", HTML.Attribute.STYLE);
        attrMap.put("title", HTML.Attribute.TITLE);
        attrMap.put("type", HTML.Attribute.TYPE);
        attrMap.put("valign", HTML.Attribute.VALIGN);
        MyParserDelegator.attrMap = Collections.unmodifiableMap(attrMap);
    }
	
	public void parse(Reader r, HTMLEditorKit.ParserCallback cb,
			boolean ignoreCharSet) throws IOException {
		ANTLRCaseInsensitiveReaderStream input = new ANTLRCaseInsensitiveReaderStream(r);
        HtmlLexer lexer = new HtmlLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        HtmlParser parser = new HtmlParser(tokens);
        parser.cb = cb;
        parser.pd = this;
        try {
			parser.html_document();
		} catch (RecognitionException e) {
			e.printStackTrace();
		}
	}
	
	public Tag strToTag(String str) {
		return tagMap.get(str.trim().toLowerCase());
	}
	
	public Attribute strToAttr(String str) {
		return attrMap.get(str.trim().toLowerCase());
	}

}
